require(["jquery","paging","template","bootstrap","echarts","bbcare","scprit/department/view/template/follow-up/common.js"],function($,Paging,template,bootstrap,echarts,$bb,$common) {
	var URL = $bb.baseUrl("department")+"followup/service";
	var obj=$bb.getPageParams();
	var userId=obj.babyId;
	var type=obj.type;//历史记录传参；
	var patientSchemeId=obj.patientSchemeId;
	var answerId = obj.answerId, result;
	var count =1, pagesize =15;
	var HashMap=$common.hashmap(),hashMap=new HashMap();
	$common.date();
	getBabyInfo();
	$(".mask, .AddInformation").hide();
	$("#historyData").click(function (event) {
		getBabyList(1, pagesize);
		var p = new Paging();
		$("#pageTool").html("");
		p.init({target:'#pageTool',pagesize:pagesize,count:count,toolbar:false,callback:function(page,size,count){
			getBabyList(page,size);
		}});
		$(".mask, .AddInformation").show();
	});
	$(".AddInformation h4 span").click(function (event) {
		$(".mask, .AddInformation").hide();
	});
	getBabyInfo();
	if(answerId != null) {
		result = JSON.parse(localStorage.getItem("healthCare"));
		getBabyClinic42(result);
	}
	$("#submitData").click(function(){
		var hashMap = new HashMap();
		hashMap.put("illHistory", $("#illHistory").val());
		hashMap.put("irritabilityDesc", $("#irritabilityDesc").val());
		hashMap.put("breastMilk1", $("#breastMilk1").val());
		hashMap.put("breastMilk2", $("#breastMilk2").val());
		hashMap.put("artificial1", $("#artificial1").val());
		hashMap.put("artificia2", $("#artificia2").val());
		hashMap.put("hybrid1", $("#hybrid1").val());
		hashMap.put("hybrid2", $("#hybrid2").val());
		hashMap.put("hybrid3", $("#hybrid3").val());
		hashMap.put("hybrid4", $("#hybrid4").val());
		hashMap.put("fecesNum", $("#fecesNum").val());
		hashMap.put("feces", $("#feces").val());
		hashMap.put("sideDish", $("#sideDish").val());
		hashMap.put("sleepTime", $("#sleepTime").val());
		hashMap.put("noNightTime", $("#noNightTime").val());
		hashMap.put("nightTime", $("#nightTime").val());
		hashMap.put("activityTime", $("#activityTime").val());
		hashMap.put("D31", $("#D31").val());
		hashMap.put("D32", $("#D32").val());
		hashMap.put("weightVal", $("#weightVal").val());
		hashMap.put("heightVal", $("#heightVal").val());
		hashMap.put("headCircumferenceVal", $("#headCircumferenceVal").val());
		hashMap.put("weightDesc", $("#weightDesc").val());
		hashMap.put("heightDesc", $("#heightDesc").val());
		hashMap.put("whDesc", $("#whDesc").val());
		hashMap.put("error", $("#error").val());
		hashMap.put("spleen", $("#spleen").val());
		hashMap.put("heartSoundsVal", $("#heartSoundsVal").val());
		hashMap.put("scarfSign2", $("#scarfSign2").val());
		hashMap.put("scarfSign3", $("#scarfSign3").val());
		hashMap.put("scarfSign4", $("#scarfSign4").val());
		hashMap.put("beili", $("#beili").val());
		hashMap.put("beili1", $("#beili1").val());
		hashMap.put("beili2", $("#beili2").val());
		hashMap.put("beili3", $("#beili3").val());
		hashMap.put("beili4", $("#beili4").val());
		hashMap.put("beili5", $("#beili5").val());
		hashMap.put("hb", $("#hb").val());
		hashMap.put("d3", $("#d3").val());
		hashMap.put("other", $("#other").val());
		hashMap.put("diagnosis", $("#diagnosis").val());

		hashMap.put("hearing", $("input[name='hearing']:checked").val());
		hashMap.put("vision", $("input[name='vision']:checked").val());
		hashMap.put("irritability", $("input[name='irritability']:checked").val());
		hashMap.put("appetite", $("input[name='appetite']:checked").val());
		hashMap.put("addSideDish", $("input[name='addSideDish']:checked").val());
		hashMap.put("sleep", $("input[name='sleep']:checked").val());
		hashMap.put("perspiration", $("input[name='perspiration']:checked").val());
		hashMap.put("nightTerror", $("input[name='nightTerror']:checked").val());
		hashMap.put("beAgitated", $("input[name='beAgitated']:checked").val());
		hashMap.put("crying", $("input[name='crying']:checked").val());
		hashMap.put("face", $("input[name='face']:checked").val());
		hashMap.put("skin", $("input[name='skin']:checked").val());
		hashMap.put("isError", $("input[name='isError']:checked").val());
		hashMap.put("oral", $("input[name='oral']:checked").val());
		hashMap.put("heartSounds", $("input[name='heartSounds']:checked").val());
		hashMap.put("lungSound", $("input[name='lungSound']:checked").val());
		hashMap.put("abdomen", $("input[name='abdomen']:checked").val());
		hashMap.put("genitals", $("input[name='genitals']:checked").val());
		hashMap.put("hip", $("input[name='hip']:checked").val());
		hashMap.put("armTattoo", $("input[name='armTattoo']:checked").val());
		hashMap.put("stockLines", $("input[name='stockLines']:checked").val());
		hashMap.put("tension1", $("input[name='tension1']:checked").val());
		hashMap.put("tension2", $("input[name='tension2']:checked").val());
		hashMap.put("scarfSign1", $("input[name='scarfSign1']:checked").val());
		hashMap.put("posture", $("input[name='posture']:checked").val());
		hashMap.put("guide", $("input[name='guide']:checked").val());

		$.ajax({
			type: "POST",
			xhrFields : {withCredentials: true},
			crossDomain : true,
			url: URL+"/comm/question/saveOutpatient",
			data: {
				"babyId" : babyId,
				"type" : "healthCare",
				"answerId":answerId,
				"result" : JSON.stringify(hashMap)
			},
			success: function(data){
				if(data.success) {
					alert("保存成功");
				} else {
					alert("保存失败");
				}
			},
		});
	});

	function getBabyInfo() {
		$.ajax({
			type: "POST",
			xhrFields : {withCredentials: true},
			crossDomain : true,
			url: URL+"/comm/baby/getBabyInfo",
			data: {
				"babyId" : babyId
			},
			success: function(data){
				if(data.success) {
					var babyInfo=data.data.PatientInfo;
					var gender= (babyInfo.gender=="N")? "男":"女";
					var babyData=$common.babyInfoDeal(babyInfo);
					$common.infoInit({"id":"#babyInfoList","data":babyData});

					$("#babyName").val(babyInfo.userName);
					$("#gender").val(gender);
					var time = new Date(babyInfo.birthday).Format("yyyy年MM月dd日");
					$("#babyBirthday").val(time);
					$("#birthWeight").val(babyInfo.weight==undefined?"未知":babyInfo.weight+"(g)");

					$("#userName").html(babyInfo.createAuthor);
					$("#nowTime").html($common.getNowFormatDate());

					if(answerId == null) {
						var babyDay = $common.DateDiff(babyInfo.birthday,  $common.getNowFormatDate());
						$("#babyDay").val(babyDay);
					}
				}
			}
		});
	}

	function getBabyClinic42(result) {
		$("#illHistory").val(result.illHistory);
		$("#irritabilityDesc").val(result.irritabilityDesc);
		$("#breastMilk1").val(result.breastMilk1);
		$("#breastMilk2").val(result.breastMilk2);
		$("#artificial1").val(result.artificial1);
		$("#artificia2").val(result.artificia2);
		$("#hybrid1").val(result.hybrid1);
		$("#hybrid2").val(result.hybrid2);
		$("#hybrid3").val(result.hybrid3);
		$("#hybrid4").val(result.hybrid4);
		$("#fecesNum").val(result.fecesNum);
		$("#feces").val(result.feces);
		$("#sideDish").val(result.sideDish);
		$("#sleepTime").val(result.sleepTime);
		$("#noNightTime").val(result.noNightTime);
		$("#nightTime").val(result.nightTime);
		$("#activityTime").val(result.activityTime);
		$("#D31").val(result.D31);
		$("#D32").val(result.D32);
		$("#weightVal").val(result.weightVal);
		$("#heightVal").val(result.heightVal);
		$("#headCircumferenceVal").val(result.headCircumferenceVal);
		$("#weightDesc").val(result.weightDesc);
		$("#heightDesc").val(result.heightDesc);
		$("#whDesc").val(result.whDesc);
		$("#error").val(result.error);
		$("#spleen").val(result.spleen);
		$("#heartSoundsVal").val(result.heartSoundsVal);
		$("#scarfSign2").val(result.scarfSign2);
		$("#scarfSign3").val(result.scarfSign3);
		$("#scarfSign4").val(result.scarfSign4);
		$("#beili").val(result.beili);
		$("#beili1").val(result.beili1);
		$("#beili2").val(result.beili2);
		$("#beili3").val(result.beili3);
		$("#beili4").val(result.beili4);
		$("#beili5").val(result.beili5);
		$("#hb").val(result.hb);
		$("#d3").val(result.d3);
		$("#other").val(result.other);
		$("#diagnosis").val(result.diagnosis);

		$("input[type=radio][name=hearing][value='"+result.hearing+"']").attr("checked",true);
		$("input[type=radio][name=vision][value='"+result.vision+"']").attr("checked",true);
		$("input[type=radio][name=irritability][value='"+result.irritability+"']").attr("checked",true);
		$("input[type=radio][name=appetite][value='"+result.appetite+"']").attr("checked",true);
		$("input[type=radio][name=addSideDish][value='"+result.addSideDish+"']").attr("checked",true);
		$("input[type=radio][name=sleep][value='"+result.sleep+"']").attr("checked",true);
		$("input[type=radio][name=perspiration][value='"+result.perspiration+"']").attr("checked",true);
		$("input[type=radio][name=nightTerror][value='"+result.nightTerror+"']").attr("checked",true);
		$("input[type=radio][name=beAgitated][value='"+result.beAgitated+"']").attr("checked",true);
		$("input[type=radio][name=crying][value='"+result.crying+"']").attr("checked",true);
		$("input[type=radio][name=face][value='"+result.face+"']").attr("checked",true);
		$("input[type=radio][name=isError][value='"+result.isError+"']").attr("checked",true);
		$("input[type=radio][name=oral][value='"+result.oral+"']").attr("checked",true);
		$("input[type=radio][name=heartSounds][value='"+result.heartSounds+"']").attr("checked",true);
		$("input[type=radio][name=lungSound][value='"+result.lungSound+"']").attr("checked",true);
		$("input[type=radio][name=abdomen][value='"+result.abdomen+"']").attr("checked",true);
		$("input[type=radio][name=genitals][value='"+result.genitals+"']").attr("checked",true);
		$("input[type=radio][name=hip][value='"+result.hip+"']").attr("checked",true);
		$("input[type=radio][name=armTattoo][value='"+result.armTattoo+"']").attr("checked",true);
		$("input[type=radio][name=stockLines][value='"+result.stockLines+"']").attr("checked",true);
		$("input[type=radio][name=tension1][value='"+result.tension1+"']").attr("checked",true);
		$("input[type=radio][name=tension2][value='"+result.tension2+"']").attr("checked",true);
		$("input[type=radio][name=scarfSign1][value='"+result.scarfSign1+"']").attr("checked",true);
		$("input[type=radio][name=posture][value='"+result.posture+"']").attr("checked",true);
		$("input[type=radio][name=guide][value='"+result.guide+"']").attr("checked",true);
	}


	//历史记录列表点击：
	$("#dataList").on('click',"ul[result]",function () {
		localStorage.setItem("healthCare",hashMap.get($(this).attr("result")));
		var obj={
			"answerId":$(this).attr("result"),
			"babyId":babyId,
			"type":type
		};
		$bb.openPage("heathyCareReport",obj);
	});
	//历史记录列表：
	function getBabyList(page, rows) {
		$.ajax({
			type: "POST",
			xhrFields: {withCredentials: true},
			crossDomain: true,
			async: false,
			url: URL + "/comm/question/getHistoryScore",
			data: {
				babyId: babyId,
				type: type,
				page: page,
				rows: rows
			},
			success: function (data) {
				if (data.success) {
					hashMap = new HashMap();
					count = data.total;
					var str = '';
					for (var i = 0; i < data.rows.length; i++) {
						hashMap.put(data.rows[i].answerId, data.rows[i].result);
						var pretermBirth = 37 * 7 > data.rows[i].pregnancyWeek ? "是" : "否";
						var nowDate =$common.getNowFormatDate();
						var monthAge = $common.DateDiff(nowDate, data.rows[i].birthday) / 30;
						var time = new Date(data.rows[i].createTime).Format("yyyy-MM-dd");
						str += '<ul class="clearfix" result="' + data.rows[i].answerId + '">';
						str += '<li>' + data.rows[i].babyName + '</li>';
						str += '<li>' + data.rows[i].birthWeight + '(g)</li>';
						str += '<li>' + data.rows[i].birthday + '</li>';
						str += '<li>' + data.rows[i].pregnancyWeek + '(天)</li>';
						str += '<li>' + pretermBirth + '</li>';
						str += '<li>' + data.rows[i].doctorName + '</li>';
						str += '<li>' + time + '</li>';
						str += '</ul>';
					}
					$("#dataList").html(str);
				}
			}
		});
	}


});